package com.sky.web.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageDTO;
import com.sky.entity.Dish;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 菜品分页
     * @param dishPageDTO
     * @return
     */
    @GetMapping("/page")
    public Result findByPage(DishPageDTO dishPageDTO){//1 接收参数  GET请求方式,参数不需要加注解
        //2 调用Service
        PageBean<DishVO> pageBean = dishService.findByPage(dishPageDTO);

        //3 返回分页
        return Result.success(pageBean);
    }

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){  //1 接收请求参数

        //2 清理缓存
        redisTemplate.delete("DISH:" + dishDTO.getCategoryId());
        //2 调用Service
        dishService.save(dishDTO);

        //3 返回结果
        return Result.success();
    }


    /**
     * 回显菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Long id){  //1 接收参数

        //2 调用Service, 返回类型VO
        DishVO dishVO = dishService.findById(id);

        return Result.success(dishVO);

    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){//1 接收参数  注解@RequestBody
        // 清理缓存
        redisTemplate.delete(redisTemplate.keys("DISH*"));
        //2 调用Service
        dishService.update(dishDTO);

        //3 返回
        return Result.success();
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result dalete(@RequestParam List<Long> ids){//1 接收参数
        // 清理缓存
        redisTemplate.delete(redisTemplate.keys("DISH*"));
        //2 调用service
        dishService.delete(ids);
        //3 返回
        return Result.success();

    }

    /**
     * 起售停售菜品
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public Result startOrStop(Long id,@PathVariable("status") Integer status){//1 接收参数@PathVariable
        // 清理缓存
        redisTemplate.delete(redisTemplate.keys("DISH*"));
        // 2 构造对象
        Dish dish = Dish.builder()
            .id(id)
            .status(status)
            .build();
        //2 调用Service
        dishService.startOrStop(dish);

        //3 返回
        return Result.success();
    }

    /**
     * 查询菜品列表
     * @param dishPageDTO
     * @return
     */
    @GetMapping("/list")
    public Result findList(DishPageDTO dishPageDTO){ //1 接收参数
        //2 调用service
        List<DishVO> list = dishService.findList(dishPageDTO);
        //3 返回结果
        return Result.success(list);
    }
}
